<?php
namespace App\Http\Middleware;

use App\Http\Controllers\Api\BaseController;
use App\Models\User\WxUser;
use App\Wen\Utils\UserUtils;
use Closure;
use Illuminate\Http\Request;

class ApiUserState{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $uid = $request->uid;
        if(_empty_($uid)){
            $baseController = new BaseController();
            return $baseController->fail(200000);
        }
        $state = UserUtils::get_user_state($uid, true);
        if($state == 1){
            $baseController = new BaseController();
            return $baseController->fail(700001);
        }if($state == 2){
            $ban_until = get_user_meta($uid, 'ban_until', 's');
            $the_user = WxUser::find($uid);
            if(time() > $ban_until && UserUtils::unlock_user($the_user, 2)){

            }else{
                $baseController = new BaseController();
                return $baseController->fail(700002);
            }

        }
        return $next($request);
    }
}
